package com.cloudcc.mobile.view.nearby;


import android.content.ClipboardManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.bus.MessageNumX;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.TianYanEntity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.SystemUtils;
import com.cloudcc.mobile.util.ToastUtil;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.fragment.MenuRightFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.cloudcc.mobile.R.id.message_num_99;
import static com.cloudcc.mobile.R.id.message_num_tz;

/**
 * Created by lin on 2017/8/1.
 */

public class NearByCompanyActivity extends BaseActivity implements View.OnClickListener , IEventLife {

    @Bind(R.id.defualt_topbar_back)
    ImageView defualtTopbarBack;
    @Bind(R.id.defualt_topbar_title)
    TextView defualtTopbarTitle;
    @Bind(R.id.near_by_com_name)
    TextView nearByComName;
    @Bind(R.id.near_by_com_state)
    TextView nearByComState;
    @Bind(R.id.near_by_com_hangye)
    TextView nearByComHangye;
    @Bind(R.id.near_by_com_callphone_tv)
    TextView nearByComCallphoneTv;
    @Bind(R.id.near_by_com_wangzhi_tv)
    TextView nearByComWangzhiTv;
    @Bind(R.id.near_by_com_dizhi_tv)
    TextView nearByComDizhiTv;
    @Bind(R.id.near_by_com_email_tv)
    TextView nearByComEmailTv;
    @Bind(R.id.near_by_com_farenname_tv)
    TextView nearByComFarennameTv;
    @Bind(R.id.near_by_com_ziben_tv)
    TextView nearByComZibenTv;
    @Bind(R.id.near_by_com_datetime_tv)
    TextView nearByComDatetimeTv;


    //消息数量
    @Bind(R.id.message_num_tz)
    TextView message_num_tz;
    @Bind(R.id.message_num_99)
    TextView message_num_99;
    @Bind(R.id.menu_filter)
    ImageView menuFilter;

    private TianYanEntity.DataBean dataBean;

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        initLister();
        initMenu();

        dataBean = (TianYanEntity.DataBean) getIntent().getBundleExtra("databundle").getSerializable("data");
        String names = dataBean.getName().replaceAll("<em>", "");
        names = names.replaceAll("</em>", "");
        nearByComName.setText(names);
        nearByComState.setText(dataBean.getRegStatus());
        nearByComHangye.setText(dataBean.getBusinessScope());
        nearByComCallphoneTv.setText(dataBean.getPhone());
        nearByComWangzhiTv.setText(dataBean.getWebsites());
        nearByComDizhiTv.setText(dataBean.getRegLocation());
        nearByComEmailTv.setText(dataBean.getEmails());
        nearByComFarennameTv.setText(dataBean.getLegalPersonName());
        nearByComZibenTv.setText(dataBean.getRegCapital());
        nearByComDatetimeTv.setText(dataBean.getEstiblishTime());
    }

    private void initLister() {
        register();
        defualtTopbarBack.setOnClickListener(this);
        nearByComCallphoneTv.setOnClickListener(this);
        menuFilter.setOnClickListener(this);
        nearByComWangzhiTv.setOnClickListener(this);
        nearByComDizhiTv.setOnClickListener(this);
        nearByComEmailTv.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MessageSetNCL();
    }

    private void MessageSetNCL(){
        long messagenum = 0;
        messagenum= RunTimeManager.getInstance().getMessageNx();
        message_num_tz.setText(messagenum+"");

        if(messagenum>0) {
            message_num_tz.setVisibility(View.VISIBLE);
            if(messagenum>99) {
                message_num_tz.setVisibility(View.INVISIBLE);
                message_num_99.setVisibility(View.VISIBLE);
            }

        }else {
            message_num_tz.setVisibility(View.INVISIBLE);
            message_num_99.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void register() {
        EventEngine.register(this);
    }

    @Override
    public void unRegister() {
        EventEngine.uregister(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nearby_company;
    }

    public void onEventMainThread(MessageNumX event) {

        message_num_tz.setText(event.messageNumX+"");
        if(event.messageNumX>0) {
            message_num_tz.setVisibility(View.VISIBLE);
            if(event.messageNumX>99) {
                message_num_tz.setVisibility(View.INVISIBLE);
                message_num_99.setVisibility(View.VISIBLE);
            }

        }else {
            message_num_tz.setVisibility(View.INVISIBLE);
            message_num_99.setVisibility(View.INVISIBLE);
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO: add setContentView(...) invocation
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    public SlidingMenu menu_R;

    private void initMenu() {
        Fragment leftMenuFragment = new MenuRightFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.id_Right_menu_frame, leftMenuFragment).commit();
        menu_R = new SlidingMenu(this);
        menu_R.setMode(SlidingMenu.RIGHT);
        menu_R.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); // 触摸边界拖出菜单
        menu_R.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu_R.setOffsetFadeDegree(0.4f);
        menu_R.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        View viewRi = LayoutInflater.from(this).inflate(R.layout.right_menu_id,
                null);
        menu_R.setMenu(viewRi);
    }


    @Override
    public void onClick(View v) {

        if (v == defualtTopbarBack) {//返回
            finish();
        }
        if (v == menuFilter) {//通知中心

            menu_R.showMenu();
        }
        if (v == nearByComCallphoneTv) {//电话
            String phone = dataBean.getPhone();
            if (phone != null) {
                phone = phone.substring(0, phone.indexOf(";"));
                SystemUtils.sendCall(this, phone);
            }
        }
        if (v == nearByComWangzhiTv) {//网址
            SystemUtils.startUrl(this, dataBean.getWebsites());
        }
        if (v == nearByComDizhiTv) {//地址
            SystemUtils.sendMap(this, 0, 0, dataBean.getRegLocation());
        }
        if (v == nearByComEmailTv) {//邮箱
            ClipboardManager clip = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
            //clip.getText(); // 粘贴
            clip.setText(nearByComEmailTv.getText()); // 复制
            LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.copy_toast_layout, null);
            ToastUtil toastUtil = new ToastUtil(this,view, Toast.LENGTH_SHORT);
            toastUtil.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegister();
    }
}
